import physics.Vect;

import java.awt.*;

/**
 * Created by obila_000 on 2014/10/20.
 */
public class BouncingBall {
    private final static double VELOCITY_STEP = 2.0;

    private float x = (float) ((Math.random() * 100.0) + 100.0);

    private float y = (float) ((Math.random() * 100.0) + 100.0);

    private float vx = (float) ((Math.random() * VELOCITY_STEP) + VELOCITY_STEP);

    private float vy = (float) ((Math.random() * VELOCITY_STEP) + VELOCITY_STEP);

    private Vect ballVect = new Vect(VELOCITY_STEP,VELOCITY_STEP);

    private float radius = 6;

    private Color color = new Color(255, 0, 0);

    private boolean isCollisionWithGizmos = false;

    // Keep track of the animation window that will be drawing this ball.
    private AnimationWindow win;

    private String ballId;

    /**
     * Constructor.
     * @param win Animation window that will be drawing this ball.
     */
    public BouncingBall(AnimationWindow win) {
        this.win = win;
    }

    /**
     * 构造函数
     * @param win
     * @param x
     * @param y
     * @param vx
     * @param vy
     */
    public BouncingBall(AnimationWindow win,float x,float y, float vx, float vy){
        this.win = win;
        this.x = x;
        this.y = y;
        this.ballVect = new Vect(vx,vy);
    }

    /**
     * @modifies this
     * @effects Moves the ball according to its velocity.  Reflections off
     * walls cause the ball to change direction.
     */
    public void move() {
        //vx += ballVect.x();
        x += vx;
        if (x <= radius) {
            x = radius;
            vx = -vx;
        }

        if (x >= win.getWidth() - radius) {
            x = win.getWidth() - radius;
            vx = -vx;
        }

        //vy += ballVect.y();
        y += vy;
        if (y <= radius) {
            y = radius;
            vy = -vy;
        }
        if (y >= win.getHeight() - radius) {
            y = win.getHeight() - radius;
            vy = -vy;
        }
    }

    /**
     * @modifies this
     * @effects Changes the velocity of the ball by a random amount
     */
    public void randomBump() {
//        vx += (int) ((Math.random() * VELOCITY_STEP) - (VELOCITY_STEP/2));
//        vx = -vx;
//        vy += (int) ((Math.random() * VELOCITY_STEP) - (VELOCITY_STEP/2));
//        vy = -vy;
    }

    /**
     * 为小球添加力的作用
     * @param xForce x方向上的力
     * @param yForce y方向上的力
     */
    public void addForce(float xForce,float yForce){
        vx+=xForce;
        vy-=yForce;
    }

    /**
     * @modifies the Graphics object <g>.
     * @effects paints a circle on <g> reflecting the current position
     * of the ball.
     * @param g Graphics context to be used for drawing.
     */
    public void paint(Graphics g) {

        // the "clip rectangle" is the area of the screen that needs to be
        // modified
        Rectangle clipRect = g.getClipBounds();
        // For this tiny program, testing whether we need to redraw is
        // kind of silly.  But when there are lots of objects all over the
        // screen this is a very important performance optimization
        if (clipRect.intersects(this.boundingBox())) {
            g.setColor(color);
            g.fillOval((int)(x - radius), (int)(y - radius), (int)(radius + radius),(int)( radius
                    + radius));
        }
    }

    /**
     * @return the smallest rectangle that completely covers the current
     * position of the ball.
     */
    public Rectangle boundingBox() {
        // a Rectangle is the x,y for the upper left corner and then the
        // width and height
        return new Rectangle((int)(x - radius - 1),(int)( y - radius - 1),(int)( radius + radius + 2),
                (int)(radius + radius + 2));
    }

    /**
     * 得到小球中心点的坐标
     * @return 小球中心点的坐标
     */
    public Point getBallCenterPos(){
        Point ballPos = new Point((int)(x),(int)(y));
        return ballPos;
    }

    /**
     * 得到小球的半径
     * @return
     */
    public float getRadius(){
        return radius;
    }

    public void setCollision(boolean isCollisionWithGizmos){
        this.isCollisionWithGizmos = isCollisionWithGizmos;
    }

    public void setRadius(float radius){
        this.radius = radius;
    }

    public void setBallId(String ballId){
        this.ballId = ballId;
    }

    public String getBallId(){
        return ballId;
    }

    public float getVx(){return this.vx;}

    public float getVy(){return this.vy;}

    public void setVect(Vect vect){
        this.vx = (float)vect.x();
        this.vy = (float)vect.y();
    }

    public Vect getVect(){
        return new Vect(this.vx,this.vy);
    }
}

